<template>
  <div class="yt-tree" :class="treeClassList">
    <div class="yt-tree-search" :style="contentStyle">
      <el-input
        class="yt-tree-input"
        placeholder="请输入搜索内容"
        v-model="keyword"
        maxlength="20"
        clearable
        @keydown.enter.native="handleSearch"
      >
      </el-input>
      <el-tree
        ref="tree"
        class="expand-tree"
        v-if="isLoadingTree"
        :node-key="nodeKey"
        :data="data"
        :props="defaultProps"
        :render-content="renderCurrContent"
        :filter-node-method="filterNode"
        :load="loadNode"
        :default-checked-keys="defaultCheckedKeys"
        :default-expended-keys="defaultExpendedKeys"
        :highlight-current="highlightCurrent"
        @check="handleBoxCheck"
        @node-click="handleNodeClick"
        @node-contextmenu="onNodeContextmenu"
        @node-drag-end="handleDragEnd"
        :allow-drop="allowDrop"
        :lazy="lazy"
        v-loading="loading"
        v-bind="$attrs"
      >
      </el-tree>
    </div>
  </div>
</template>

<script>
export default {
  name: 'yt-tree',
  props: {
    width: {
      type: [Number, String],
      default: 280
    },
    nodeKey: {
      type: String,
      default: 'value'
    },
    treeData: {
      type: Array,
      default: () => []
    },
    defaultProps: {
      type: Object,
      default: () => {
        return {
          label: 'label'
        }
      }
    },
    hasFilter: {
      type: Boolean,
      default: false
    },
    position: {
      type: String,
      default: 'center'
    },
    hasExpendBtn: {
      type: Boolean,
      default: false,
    },
    highlightCurrent: {
      type: Boolean,
      default: true
    },
    defaultCheckedKeys: {
      type: Array,
      default: () => []
    },
    defaultExpendedKeys: {
      type: Array,
      default: () => []
    },
    hasRefreshBtn: {
      type: Boolean,
      default: false
    },
    lazy: {
      type: Boolean,
      default: false
    },
    isBeforeSelect: {
      type: Boolean,
      default: false
    },
    renderContent: {
      type: Function
    }
  },
  data() {
    return {
      loading: false,
      isexpand: false,
      keyword: '',
      maxexpandId: 10000,
      non_maxexpandId: 10000,
      isLoadingTree: false,
      data: this.treeData
    }
  },
  computed: {
    contentStyle() {
      let { width } = this;
      let style = {};
      if (typeof width === 'number') {
        style['width'] = width + 'px';
      } else {
        style['width'] = width
      }
      return style
    },
    treeClassList() {
      let list = [];
      list.push('tree-style-' + this.position);
      return list
    }
  },
  watch: {
    treeData(value) {
      this.data = value
    },
  },
  mounted() {
    this.initExpand()
  },
  methods: {
    reset() {
      this.keyword = '';
      this.handleSearch();
      this.collapseAll();
    },
    initExpand() {
      this.isLoadingTree = true;
    },
    handleSearch() {
      this.$refs.tree.filter(this.keyword);
    },
    filterNode(value, data) {
      if (!value) return true;
      return data[this.defaultProps.label].indexOf(value) !== -1;
    },
    renderCurrContent(h, { node, data, store }) {
      return this.renderContent
        ? this.renderContent(h, { node, data, store })
        : this.renderContent(h, { node, data, store })
    },
    handleAdd(store, data, node) {
      this.$emit('handleAdd', store, data, node)
    },
    handleEdit(store, data, node) {
      this.$emit('handleEdit', store, data, node)
    },
    handleDelete(store, data, node) {
      this.$emit('handleDelete', store, data, node)
    },
    handleBoxCheck(event, { checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys }) {
      if (this.isBeforeSelect) {
        this.beforeSelect(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys);
      } else {
        this.$emit('handleBoxCheck', checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys)
      }
    },
    nodeContextmenu(event, data, node, store) {
      this.$emit('nodeContextmenu', event, data, node, store)
    },
    insertAfter(data, refNode) {
      this.$refs.tree.insertAfter(data, refNode);
    },
    remove(data) {
      this.$refs.tree.remove(data);
    },
    loadNode(node, resolve) {
      this.$emit('loadNode', node, resolve);
    },
    expandAll() {
      this.isexpand = true;
      const tree = this.$refs.tree;
      let data = tree.store._getAllNodes();
      if (this.defaultExpendedKeys.length) {
        data.forEach(node => {
          node.expanded = this.defaultExpendedKeys.includes(node.data[this.nodeKey])
        })
      } else {
        data.forEach(node => {
          node.expanded = this.isexpand
        })
      }
    },
    collapseAll() {
      this.isexpand = false;
      const tree = this.$refs.tree;
      let data = tree.store._getAllNodes();
      data.forEach(node => {
        node.expanded = this.isexpand
      })
    },
    toggleExpand() {
      this.isexpand = !this.expand;
      const tree = this.$refs.tree;
      let data = tree.store._getAllNodes();
      data.forEach(node => {
        node.expanded = this.isexpand
      })
    },
    refreshAllLoadTree() {
      this.loading = true;
      setTimeout(res => {
        this.loading = false;
      }, 300)
      this.$emit('refresh');
    },
    beforeSelect(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys) {
      this.$confirm('是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$emit('handleBoxCheck', checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys)
      }).catch(() => {
        this.$refs.tree.setCheckedKeys(this.defaultCheckedKeys);
      })
      return false;
    },
    handleDragEnd(draggingNode) {
      this.$emit('handleDragEnd', draggingNode)
    },
    allowDrop(draggingNode, dropNode, type) {
      return false
    }
  }
}
</script>

<style lang="less">
.yt-tree {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  &.tree-style-left {
    justify-content: flex-start;
  }
  &.tree-style-center {
    justify-content: center;
  }
  &.tree-style-right {
    justify-content: flex-end;
  }
  .yt-tree-content {
    width: 100%;
  }
  .yt-tree-search {
    width: 100%;
    display: flex;
    position: relative;
    line-height: normal;
  }
  .yt-tree-extra {
    width: 100%;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .yt-button {
      flex: 1;
      .iconfont {
        margin-right: 5px;
      }
    }
  }
  .el-input_icon {
    cursor: pointer;
  }
  .yt-tree-content {
    flex-direction: column;
  }
  .expand-tree {
    border: none;
    margin-top: 10px;
    .el-tree-node {
      .is-current {
        overflow: hidden;
        .el-tree-node__content {
          .tree-btn {
            display: inline-block;
          }
          .tree-label {
            white-space: normal;
          }
        }
      }
      &:hover {
        overflow: hidden;
      }
    }
    .el-tree-node__content {
      &:hover {
        .tree-btn {
          display: inline-block;
        }
      }
    }
  }
}
</style>